package com.scmt.hmzd.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.scmt.hmzd.entity.HmProjectTechnician;
import com.scmt.hmzd.service.IHmProjectTechnicianService;
import com.scmt.core.common.vo.PageVo;
import com.scmt.core.common.vo.SearchVo;
import com.scmt.hmzd.mapper.HmProjectTechnicianMapper;
import com.scmt.core.utis.FileUtil;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;
import java.util.List;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

/**
 * @author
 **/
@Service
public class HmProjectTechnicianServiceImpl extends ServiceImpl<HmProjectTechnicianMapper, HmProjectTechnician> implements IHmProjectTechnicianService {
    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private HmProjectTechnicianMapper hmProjectTechnicianMapper;

    @Override
    public IPage<HmProjectTechnician> queryHmProjectTechnicianListByPage(HmProjectTechnician hmProjectTechnician, SearchVo searchVo, PageVo pageVo) {
        int page = 1;
        int limit = 10;
        if (pageVo != null) {
            if (pageVo.getPageNumber() != 0) {
                page = pageVo.getPageNumber();
            }
            if (pageVo.getPageSize() != 0) {
                limit = pageVo.getPageSize();
            }
        }
        Page<HmProjectTechnician> pageData = new Page<>(page, limit);
        QueryWrapper<HmProjectTechnician> queryWrapper = new QueryWrapper<>();
        if (hmProjectTechnician != null) {
            queryWrapper = LikeAllField(hmProjectTechnician, searchVo);
        }
        IPage<HmProjectTechnician> result = hmProjectTechnicianMapper.selectPage(pageData, queryWrapper);
        return result;
    }

    @Override
    public void download(HmProjectTechnician hmProjectTechnician, HttpServletResponse response) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        QueryWrapper<HmProjectTechnician> queryWrapper = new QueryWrapper<>();
        if (hmProjectTechnician != null) {
            queryWrapper = LikeAllField(hmProjectTechnician, null);
        }
        List<HmProjectTechnician> list = hmProjectTechnicianMapper.selectList(queryWrapper);
        for (HmProjectTechnician re : list) {
            Map<String, Object> map = new LinkedHashMap<>();
            mapList.add(map);
        }
        FileUtil.createExcel(mapList, "exel.xlsx", response);
    }

    /**
     * 功能描述：构建模糊查询
     *
     * @param hmProjectTechnician 需要模糊查询的信息
     * @return 返回查询
     */
    public QueryWrapper<HmProjectTechnician> LikeAllField(HmProjectTechnician hmProjectTechnician, SearchVo searchVo) {
        QueryWrapper<HmProjectTechnician> queryWrapper = new QueryWrapper<>();
        if (hmProjectTechnician.getId() != null) {
            queryWrapper.and(i -> i.eq("hm_project_technician.id", hmProjectTechnician.getId()));
        }
        if (StringUtils.isNotBlank(hmProjectTechnician.getProjectId())) {
            queryWrapper.and(i -> i.eq("hm_project_technician.project_id", hmProjectTechnician.getProjectId()));
        }
        if (StringUtils.isNotBlank(hmProjectTechnician.getTechnicianId())) {
            queryWrapper.and(i -> i.eq("hm_project_technician.technician_id", hmProjectTechnician.getTechnicianId()));
        }
        return queryWrapper;
    }
}
